//
//  HRZCommodityGoodsViewController.m
//  EMarket
//
//  Created by 石头 on 2018/5/16.
//  Copyright © 2018年 好日子. All rights reserved.
//

#import "HRZCommodityGoodsViewController.h"

// Controllers
#import "HRZClassifyGoodsListViewController.h"

// Models
#import "HRZGoodsClassifyBaseModel.h"
#import "HRZGoodsClassifyContentModel.h"

// Views
#import "HRZClassifyHeardView.h"
#import "HRZ_GoodsSortCell.h"
#import "HRZ_BrandSortCell.h"
// Vendors
#import <MJExtension.h>
// Categories

// Others
#import "HRZGlobalNetManager.h"

static NSString *const HRZClassifyHeardViewID = @"HRZClassifyHeardView";
static NSString *const HRZGoodsSortCellID = @"HRZ_GoodsSortCell";
static NSString *const HRZBrandSortCellID = @"HRZ_BrandSortCell";

@interface HRZCommodityGoodsViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
{
    HRZGoodsClassifyContentModel *_contentModel;
}

@property (nonatomic, strong) UICollectionView *collectionView;

@end

@implementation HRZCommodityGoodsViewController

@synthesize classifyModel = _classifyModel;

#pragma mark - LazyLoad
- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
        layout.minimumInteritemSpacing = 3;
        layout.minimumLineSpacing = 5;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        [self.view addSubview:_collectionView];
        
        _collectionView.frame = CGRectMake(0, 0, kScreenWidth - kLeftViewWidth, kScreenHeight - SafeAreaTopHeight - SafeAreaBottomHeight - HRZBottomTabH);
        
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.alwaysBounceVertical = YES;
        
        //  注册cell
        [_collectionView registerClass:[HRZ_GoodsSortCell class] forCellWithReuseIdentifier:HRZGoodsSortCellID];
        [_collectionView registerClass:[HRZ_BrandSortCell class] forCellWithReuseIdentifier:HRZBrandSortCellID];
        
        //  注册header
        [_collectionView registerNib:[UINib nibWithNibName:@"HRZClassifyHeardView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:HRZClassifyHeardViewID];
    }
    return _collectionView;
}

#pragma mark - LifeCyle

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do view setup here.
    
    [self setupUI];
    
    [self setupData];
    
}

#pragma mark - Public

#pragma mark - Private
/** 设置UI */
- (void)setupUI {
    self.collectionView.backgroundColor = [UIColor whiteColor];
}

/** 获取数据 */
- (void)setupData {
    
    WEAKSELF
    [[HRZGlobalNetManager getGlobalNetManager] onSearchGoodsClassifyContentByClassifyId:_classifyModel.modelId complete:^(HRZGoodsClassifyContentModel *result, BOOL isSuc) {
        
        if (isSuc && result) {
            
            _contentModel = result;
        } else {
            if (!_contentModel) {
                [HRZGoodsClassifySecondModel mj_setupReplacedKeyFromPropertyName:^NSDictionary *{
                    return @{
                             @"threeClassify":@"goods"
                             };
                }];
                [HRZGoodsClassifyBaseModel mj_setupReplacedKeyFromPropertyName:^NSDictionary *{
                    return @{
                             @"classifyImg":@"image_url"
                             };
                }];
                _contentModel = [HRZGoodsClassifyContentModel mj_objectWithFilename:_classifyModel.fileName];
            }
        }
        [weakSelf.collectionView reloadData];
    }];
}

#pragma mark - Getter

#pragma mark - Setter
- (void)setClassifyModel:(HRZGoodsClassifyBaseModel *)classifyModel {
    _classifyModel = classifyModel;
}

#pragma mark - ----------  method  ----------


#pragma mark - -------------<UICollectionViewDataSource>-------------
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    if (!_contentModel.classifyArray) {
        return 1;
    }
    return _contentModel.classifyArray.count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    NSInteger count = _contentModel.classifyArray[section].subArray.count;
    return count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *gridcell;
    HRZClassifyTypeModel *typeModel = _contentModel.classifyArray[indexPath.section];
    
    switch (typeModel.type) {
        case ClassifyTypeSearch:
        {
            HRZ_GoodsSortCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:HRZGoodsSortCellID forIndexPath:indexPath];
            cell.model = typeModel.subArray[indexPath.item];
            gridcell = cell;
        }
            break;
        case ClassifyTypeClassify:
        {
            HRZ_GoodsSortCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:HRZGoodsSortCellID forIndexPath:indexPath];
            cell.model = typeModel.subArray[indexPath.item];
            gridcell = cell;
            
        }
            break;
        case ClassifyTypeBrand:
        {
            HRZ_BrandSortCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:HRZBrandSortCellID forIndexPath:indexPath];
            HRZGoodsClassifyBrandModel *brandModel = typeModel.subArray[indexPath.item];
            
            cell.model = brandModel;
            gridcell = cell;
            
        }
            break;
    }
    
    return gridcell;
}

#pragma mark - ------<UICollectionViewDelegate>-------
#pragma mark - HeaderView
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    UICollectionReusableView *reusableView;
    
    if (kind == UICollectionElementKindSectionHeader) {
        HRZClassifyHeardView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:HRZClassifyHeardViewID forIndexPath:indexPath];
        HRZClassifyTypeModel *typeModel = _contentModel.classifyArray[indexPath.section];
        headerView.model = typeModel;
        
        reusableView = headerView;
    } 
    
    return reusableView;
}

#pragma mark - ItemSize
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
//    if (indexPath.section == 0) {
//        return CGSizeMake(KScreenWidth - kLeftViewWidth - 20, 80);
//    }
    return CGSizeMake((kScreenWidth - kLeftViewWidth - 6 - HRZMargin) / 3, (kScreenWidth - kLeftViewWidth - 6 - HRZMargin) / 3 + 20);
}

#pragma mark - Header宽高
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return CGSizeZero;
    }
    return CGSizeMake(kScreenWidth - kLeftViewWidth, 30);
}

#pragma mark - foot宽高
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
    return CGSizeZero;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    HRZClassifyGoodsListViewController *goodsSetViewC = [[HRZClassifyGoodsListViewController alloc] init];
    HRZClassifyTypeModel *typeModel = _contentModel.classifyArray[indexPath.section];
    
    
    HRZGoodsClassifyBaseModel *baseModel = typeModel.subArray[indexPath.item];
    
    NSString *searchKey;
    NSString *titleName;
    GoodsListSearchType type;
    
    switch (typeModel.type) {
        case ClassifyTypeSearch:
            type = GoodsListSearchTypeSearch;
            searchKey = typeModel.name;
            titleName = baseModel.className;
            break;
        case ClassifyTypeClassify:
            type = GoodsListSearchTypeClassify;
            searchKey = typeModel.modelId;
            titleName = baseModel.className;
            break;
        case ClassifyTypeBrand:
            type = GoodsListSearchTypeBrand;
            searchKey = typeModel.modelId;
            HRZGoodsClassifyBrandModel *brandModel = typeModel.subArray[indexPath.item];
            titleName = brandModel.tkName;
            break;
    }
    goodsSetViewC.type = type;
    goodsSetViewC.searchKey = searchKey;
    goodsSetViewC.titleName = titleName;

    [self.navigationController pushViewController:goodsSetViewC animated:YES];
}

@end
